//
//  NSObject+ZCLeaks.m
//  Pods-ZCXiaoXinFenCategory_Example
//
//  Created by 张葱 on 2019/11/7.
//

#import "NSObject+ZCLeaks.h"
#import <objc/runtime.h>
@implementation NSObject (ZCLeaks)

/**
 对象交换方法

 @param oriSEL 原始的方法
 @param swizzledSEL 新交换的方法
 @return 交换结果
 */
+(BOOL)lg_hookOrigInstanceMenthod:(SEL)oriSEL newInstanceMenthod:(SEL)swizzledSEL{
    Class cls = self;
    Method oriMethod = class_getInstanceMethod(cls, oriSEL);
    Method swiMethod = class_getInstanceMethod(cls, swizzledSEL);
    
    if (!swiMethod) {
        return NO;
    }
    
    if (!oriMethod) {
        class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
        method_setImplementation(swiMethod, imp_implementationWithBlock(^(id self, SEL _cmd){ }));
    }
    
    BOOL didAddMethod = class_addMethod(cls, oriSEL, method_getImplementation(swiMethod), method_getTypeEncoding(swiMethod));
    if (didAddMethod) {
        class_replaceMethod(cls, swizzledSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
    }else{
        method_exchangeImplementations(oriMethod, swiMethod);
    }
    return YES;
}

-(void)lg_willDealloc{
    __weak id weakSelf = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        __strong  id strongSelf = weakSelf;
        NSLog(@"来了");
        [strongSelf assertNotDealloc];
        
    });
}


- (void) assertNotDealloc {
    NSLog(@"zc--------------Leaks %@", NSStringFromClass([self class]));
}
@end
